package com.seannews.client.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.seannews.server.Pojo.Basicinfo;
import com.seannews.server.Pojo.Basicinfo2;
import com.seannews.server.Pojo.News;
import com.seannews.server.Pojo.Newstype;
import com.seannews.server.Service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/news")
@CrossOrigin
@Slf4j
public class NewsController {
    @Resource
    private NewsService newsService;

    @CrossOrigin
    @GetMapping("/getNewsbytype")
    @ResponseBody
    public List<News> getNews(String type)
    {
        return newsService.getNewsList(type);
    }
    @CrossOrigin
    @GetMapping("/getNewsall")
    @ResponseBody
    public String getNewsall(int page,int limit,String title,String newsId,String category) throws JsonProcessingException {
        if(title.equals("")) title=null;
        if(newsId.equals("")) newsId=null;
        if(category.equals("")) category=null;
        List<News> data=newsService.getNewsall((page-1)*limit,limit,title,newsId,category);
        ObjectMapper om=new ObjectMapper();
        String json=om.writeValueAsString(data);
        String a="{\"code\":0,\"count\":"+newsService.getnewscount()+",\"data\":"+json+",\"msg\":\"\"}";
        return a;
    }
    @CrossOrigin
    @GetMapping("/getNewstypecount")
    @ResponseBody
    public List<Newstype> getNewstypecount()
    {
        return newsService.getNewstypecount();
    }
    @CrossOrigin
    @GetMapping("/getNewsbyid")
    @ResponseBody
    public News getNewsbyid(String newsId,String userId)
    {
        log.info("调用：getNewsbyid，userId="+userId);
        return newsService.getNewbyid(newsId,userId);
    }
    @CrossOrigin
    @GetMapping("/getbasicinfo")
    @ResponseBody
    public Basicinfo getbasicinfo()
    {
        return newsService.getBasicinfo();
    }
    @CrossOrigin
    @GetMapping("/getbasicinfo2")
    @ResponseBody
    public Basicinfo2 getbasicinfo2()
    {
        return newsService.getBasicinfo2();
    }
    @CrossOrigin
    @GetMapping("/getvisitbytype")
    @ResponseBody
    public List<Newstype> getvisitbytype()
    {
        return newsService.getvisitbytype();
    }

    @CrossOrigin
    @GetMapping("/getdatecount")
    @ResponseBody
    public List<Newstype> getdatecount()
    {
        return newsService.getdatecount();
    }
    @CrossOrigin
    @GetMapping("/deletenews")
    @ResponseBody
    public void deletenews(String newsId)
    {
        newsService.deletenews(newsId);
    }
}
